import __builtin__
from direct.showbase.DirectObject import DirectObject
from panda3d.core import Vec3

ACCELERATION_KEY_MOVEMENT = 0.5
DECELERATION_KEY_MOVEMENT = 0.2
MAX_VELOCITY_KEY_MOVEMENT = 0.1


class InputManager(DirectObject):
    def __init__(self):
        print("INIT INPUTMANAGER")
        self.keys = {"moveLeft": 0, "moveRight": 0, "moveUp": 0, "moveDown": 0}
        self.curAcc = Vec3(0, 0, 0)
        self.border = 10

    def activateKeyMovement(self):
        self.accept("arrow_left",     self.setKey, ["moveLeft", 1])
        self.accept("arrow_left-up",  self.setKey, ["moveLeft", 0])
        self.accept("arrow_right",    self.setKey, ["moveRight", 1])
        self.accept("arrow_right-up", self.setKey, ["moveRight", 0])
        self.accept("arrow_up",     self.setKey, ["moveUp", 1])
        self.accept("arrow_up-up",  self.setKey, ["moveUp", 0])
        self.accept("arrow_down",    self.setKey, ["moveDown", 1])
        self.accept("arrow_down-up", self.setKey, ["moveDown", 0])

    def disableKeyMovement(self):
        # remove arrow listener
        self.ignore("arrow_left")
        self.ignore("arrow_left-up")
        self.ignore("arrow_right")
        self.ignore("arrow_right-up")
        self.ignore("arrow_up")
        self.ignore("arrow_up-up")
        self.ignore("arrow_down")
        self.ignore("arrow_down-up")

        # reset vars
        self.keys = {"moveLeft": 0, "moveRight": 0, "moveUp": 0, "moveDown": 0}
        self.curAcc = Vec3(0, 0, 0)

    # set given key to value
    def setKey(self, key, val):
        self.keys[key] = val

    # update inputmanager
    def update(self, dt):
        # move left
        if self.keys["moveRight"]:
            if self.curAcc.x < MAX_VELOCITY_KEY_MOVEMENT:
                self.curAcc.x += ACCELERATION_KEY_MOVEMENT * dt
        elif self.curAcc.x > 0:
            self.curAcc.x -= DECELERATION_KEY_MOVEMENT * dt

        # move right
        if self.keys["moveLeft"]:
            if self.curAcc.x > -MAX_VELOCITY_KEY_MOVEMENT:
                self.curAcc.x -= ACCELERATION_KEY_MOVEMENT * dt
        elif self.curAcc.x < 0:
            self.curAcc.x += DECELERATION_KEY_MOVEMENT * dt

        # move up
        if self.keys["moveDown"]:
            if self.curAcc.y > -MAX_VELOCITY_KEY_MOVEMENT:
                self.curAcc.y -= ACCELERATION_KEY_MOVEMENT * dt
        elif self.curAcc.y < 0:
            self.curAcc.y += DECELERATION_KEY_MOVEMENT * dt

        # move down
        if self.keys["moveUp"]:
            if self.curAcc.y < MAX_VELOCITY_KEY_MOVEMENT:
                self.curAcc.y += ACCELERATION_KEY_MOVEMENT * dt
        elif self.curAcc.y > 0:
            self.curAcc.y -= DECELERATION_KEY_MOVEMENT * dt

        # remove small numbers
        if ACCELERATION_KEY_MOVEMENT * dt > self.curAcc.x > -ACCELERATION_KEY_MOVEMENT * dt:
            self.curAcc.x = 0

        if ACCELERATION_KEY_MOVEMENT * dt > self.curAcc.y > -ACCELERATION_KEY_MOVEMENT * dt:
            self.curAcc.y = 0

        # check borders
        if __builtin__.base.camCenter.getPos().x + self.curAcc.x > 20 or __builtin__.base.camCenter.getPos().x + self.curAcc.x < -20:
            self.curAcc.x = 0

        if __builtin__.base.camCenter.getPos().y + self.curAcc.y > 20 or __builtin__.base.camCenter.getPos().y + self.curAcc.y < -20:
            self.curAcc.y = 0

        # update cam pos
        __builtin__.base.camCenter.setPos(__builtin__.base.camCenter.getPos() + self.curAcc)
        #print __builtin__.base.camCenter.getPos()

